package impl;

import anaydis.sort.SorterType;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class QuickSorterMedianOfThree extends AbstractSorter{

    public QuickSorterMedianOfThree(){
        super(SorterType.QUICK_MED_OF_THREE);
    }

    public <T> void sort(Comparator<T> tComparator, List<T> ts){
        List<Integer> pivotes = new ArrayList<Integer>();
        pivotes.add(0);
        pivotes.add(ts.size());
        int i = 0;
        while((i*2)+1<pivotes.size()){
            if (Math.abs(pivotes.get((2*i)+1) - pivotes.get(2*i)) <= 2) {
            } else {
                int pivoteAux;
                int pivoteAux2;
                int pivote;
                int indice;
                if(pivotes.get((2*i)+1) - pivotes.get(2*i) > 0){
                    indice = pivotes.get(2*i);
                }
                else{
                    indice = pivotes.get((2*i)+1);
                }

                if(greater(tComparator, ts, indice, indice+1)){
                    pivoteAux = indice;
                }
                else{
                    pivoteAux = indice+1;
                }
                if(greater(tComparator, ts, indice+1, indice+2)){
                    pivoteAux2 = indice+1;
                }
                else{
                    pivoteAux2 = indice+2;
                }
                if(greater(tComparator, ts, pivoteAux, pivoteAux2)){
                    pivote = pivoteAux2;
                }
                else{
                    pivote = pivoteAux;
                }
                int g = partition(tComparator, ts, pivotes.get(2*i) , pivotes.get((2*i)+1), pivote);
                pivotes.add(pivotes.get(2*i));
                pivotes.add(g);
                pivotes.add(g);
                pivotes.add(pivotes.get((2*i)+1));
            }
            i++;
        }
    }


    protected <T> int partition(Comparator<T> tComparator, List<T> ts,int desde, int hasta, int pivote){
        int i = desde;
        int j = hasta-2;
        boolean end = false;
        swap(ts, pivote, hasta-1);
        pivote = hasta-1;
        do{
            boolean imayor = false;
            do{
                if (!greater(tComparator, ts, i,pivote)) {
                    i++;
                } else {
                    imayor = true;
                }
            }while((!imayor) && (i < (j + 1)));
                boolean jmenor = false;
                do{
                    if (greater(tComparator, ts, j,pivote)) {
                        j--;
                    } else {
                        jmenor = true;
                    }
                }while((!jmenor) && i<j+1);
                if(i<j){
                    swap(ts, i, j);
                }
                else{
                    swap(ts,i,pivote);
                    end = true;
                }
        }while(!end);
        return i;
    }

}